package com.ruoyi.zsyh.controller;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.zsyh.domain.ZsyhAgentOrderRel;
import com.ruoyi.zsyh.domain.ZsyhPlatformPartsattribute;
import com.ruoyi.zsyh.service.IZsyhAgentOrderRelService;
import com.ruoyi.zsyh.service.IZsyhPlatformPartsattributeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhAgentOrder;
import com.ruoyi.zsyh.service.IZsyhAgentOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大区会长预订单Controller
 * 
 * @author yuexl
 * @date 2021-12-15
 */
@Controller
@RequestMapping("/zsyh/agentOrder")
public class ZsyhAgentOrderController extends BaseController
{
    private String prefix = "zsyh/agentOrder";

    @Autowired
    private IZsyhAgentOrderService zsyhAgentOrderService;
    @Autowired
    private IZsyhAgentOrderRelService zsyhAgentOrderRelService;
    @Autowired
    private IZsyhPlatformPartsattributeService zsyhPlatformPartsattributeService;

    @RequiresPermissions("zsyh:agentOrder:view")
    @GetMapping()
    public String agentOrder()
    {
        return prefix + "/agentOrder";
    }

    @RequiresPermissions("zsyh:agentOrder:view")
    @GetMapping("/agentOrderCurrent")
    public String agentOrderCurrent()
    {
        return prefix + "/agentOrderCurrent";
    }

    /**
     * 查询大区会长预订单列表
     */
    @RequiresPermissions("zsyh:agentOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhAgentOrder zsyhAgentOrder)
    {
        startPage();
        List<ZsyhAgentOrder> list = zsyhAgentOrderService.selectZsyhAgentOrderList(zsyhAgentOrder);
        return getDataTable(list);
    }

    /**
     * 查询大区会长预订单列表
     */
    @RequiresPermissions("zsyh:agentOrder:list")
    @PostMapping("/listCurrentMonth")
    @ResponseBody
    public TableDataInfo listCurrentMonth(ZsyhAgentOrder zsyhAgentOrder)
    {
        //获取当前年月
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH )+1;
        zsyhAgentOrder.setCurrentYear(year);
        zsyhAgentOrder.setCurrentMonth(month);
        Map<String,Object> params = new HashMap<>();
        params.put("orderState","0");
        zsyhAgentOrder.setParams(params);
        List<ZsyhAgentOrder> list = zsyhAgentOrderService.selectZsyhAgentOrderCurrentList(zsyhAgentOrder);
        return getDataTable(list);
    }

    /**
     * 查询大区会长预订单列表
     */
    @RequiresPermissions("zsyh:agentOrder:list")
    @PostMapping("/listCurrentYear")
    @ResponseBody
    public TableDataInfo listCurrentYear(ZsyhAgentOrder zsyhAgentOrder)
    {
        //获取当前年月
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        zsyhAgentOrder.setCurrentYear(year);
        List<ZsyhAgentOrder> list = zsyhAgentOrderService.selectZsyhAgentOrderSum(zsyhAgentOrder);
        return getDataTable(list);
    }

    /**
     * 导出大区会长预订单列表
     */
    @RequiresPermissions("zsyh:agentOrder:export")
    @Log(title = "大区会长预订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhAgentOrder zsyhAgentOrder)
    {
        List<ZsyhAgentOrder> list = zsyhAgentOrderService.selectZsyhAgentOrderList(zsyhAgentOrder);
        ExcelUtil<ZsyhAgentOrder> util = new ExcelUtil<ZsyhAgentOrder>(ZsyhAgentOrder.class);
        return util.exportExcel(list, "大区会长预订单数据");
    }

    /**
     * 新增大区会长预订单
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存大区会长预订单
     */
    @RequiresPermissions("zsyh:agentOrder:add")
    @Log(title = "大区会长预订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhAgentOrder zsyhAgentOrder)
    {
        return toAjax(zsyhAgentOrderService.insertZsyhAgentOrder(zsyhAgentOrder));
    }

    /**
     * 修改大区会长预订单
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhAgentOrder zsyhAgentOrder = zsyhAgentOrderService.selectZsyhAgentOrderById(id);
        mmap.put("zsyhAgentOrder", zsyhAgentOrder);
        return prefix + "/edit";
    }

    /**
     * 查看详情
     */
    @GetMapping("/viewOrder/{id}")
    public String viewOrder(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhAgentOrder zsyhAgentOrder = zsyhAgentOrderService.selectZsyhAgentOrderById(id);
        mmap.put("zsyhAgentOrder", zsyhAgentOrder);
        return prefix + "/viewOrder";
    }

    /**
     * 修改大区商城配件管理
     */
    @GetMapping("/viewOrder/viewPlatformAttribute")
    public String viewAgentAttribute(String id, ModelMap mmap)
    {
        ZsyhPlatformPartsattribute zsyhPlatformPartsattribute = zsyhPlatformPartsattributeService.selectZsyhPlatformPartsattributeById(id);
        mmap.put("zsyhPlatformPartsattribute", zsyhPlatformPartsattribute);
        return prefix + "/viewPlatformAttribute";
    }

    /**
     * 修改保存大区会长预订单
     */
    @RequiresPermissions("zsyh:agentOrder:edit")
    @Log(title = "大区会长预订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhAgentOrder zsyhAgentOrder)
    {
        return toAjax(zsyhAgentOrderService.updateZsyhAgentOrder(zsyhAgentOrder));
    }

    /**
     * 删除大区会长预订单
     */
    @RequiresPermissions("zsyh:agentOrder:remove")
    @Log(title = "大区会长预订单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zsyhAgentOrderService.deleteZsyhAgentOrderByIds(ids));
    }

    /**
     * 查询订单详细列表
     */
    @RequiresPermissions("zsyh:agentOrder:remove")
    @PostMapping("/listChild")
    @ResponseBody
    public TableDataInfo listChild(String mid)
    {
        startPage();
        ZsyhAgentOrderRel zsyhAgentOrderRel = new ZsyhAgentOrderRel();
        zsyhAgentOrderRel.setOrderId(mid);
        List<ZsyhAgentOrderRel> list = zsyhAgentOrderRelService.selectZsyhAgentOrderRelList(zsyhAgentOrderRel);
        return getDataTable(list);
    }

}
